<template>
	<view class="main_container">
		<u-loading-page :loading="loading"></u-loading-page>
		<view class="bg_img">
			<view class="input_container">
				<u-textarea v-model="post_content" placeholder="输入内容" autoHeight count maxlength="200"></u-textarea>
				<u-upload customStyle="padding-top:10px;" :maxCount="9" :fileList="img_list" :previewFullImage="true" @afterRead="add_img" @delete="del_img" multiple=""></u-upload>
			</view>
		</view>
		<uview class="button_container">
			<u-button @click="" color="rgb(170, 170, 255)" size="large" customStyle="width: 60vw;" @click="handle">发送</u-button>
		</uview>
	</view>
</template>

<script>
	import utils from '@/static/uploadFile.js'
	
	export default {
		data() {
			return {
				cur_forum: "",
				post_content: "",
				img_list: [],
				loading: false
			}
		},
		onLoad(options) {
			console.log("hello post")
			if (options.id) {
				console.log(options.id)
				this.cur_forum = options.id
			}
		},
		methods: {
			add_img(c) {
				console.log(c)
				c.file.forEach( cur_img => {
					this.img_list.push({ url: cur_img.url })
				})
			},
			del_img(c) {
				this.img_list.splice(c.index, 1)
			},
			handle() {
				if (this.post_content.length >= 1 || this.img_list.length > 0) {
					this.loading = true
					this.add()
					.then(d => {
						console.log(d)
						this.post_content = ""
						this.img_list = []
						this.loading = false
						uni.navigateBack()
					})
					.catch(e => console.error(e))
				} else {
					uni.showModal({
						title: "发送空白内容是不被喜欢的哦"
					})
				}
			},
			async add() {
				let upload_urls = []
						
				await uni.request({
					url: getApp().globalData.base_url + "/add/post_message_img_url_clear",
					header: {"token" : getApp().globalData.access_token}
				})
				
				for (var _ in Array.from({ length: this.img_list.length }, (_, index) => index)) {
					try {
						let url = await uni.request({
							url: getApp().globalData.base_url + "/add/post_message_img_url",
							header: {"token" : getApp().globalData.access_token}
						})
						if (url.statusCode != 200) {
							throw "statu err"
						}
						upload_urls.push(url.data)
					} catch (error) {
						throw error
					}
				}
				
				console.log("上传列表:"+upload_urls)
				
				//upload
				for (var i in Array.from({ length: this.img_list.length }, (_, index) => index)) {
					utils.uploadImageToTencentCloud(
						this.img_list[i].url,
						upload_urls[i],
						d => console.log(d),
						e => uni.showModal({
							title: "上传失败",
							content: JSON.stringify(e)
						})
					)
				}
				
				// add_message
				let result = await uni.request({
					url: getApp().globalData.base_url + "/add/post_message/" + this.cur_forum,
					header: {"token" : getApp().globalData.access_token},
					method: "POST",
					data: { message: this.post_content}
				})
				// console.log(getApp().globalData.base_url + "/add/post_message/" + this.cur_forum,)
				
				
				
				return upload_urls
			}
		}
	}
</script>

<style lang="scss">
	.main_container {
		width: 100vw;
		padding-top: 20px;
		.bg_img {
			margin: 0 auto;
			width: 90vw;
			padding: 2px;	
			// background: linear-gradient(to right, #aaaaff, #ffffff);
			border-radius: 17px;
			.input_container {
				margin: 0 auto;
				border: 2px;
				border-radius: 15px;
				padding: 15rpx;
				padding-top: 12rpx;
				background-color: rgb(231, 237, 254);
			}
		}
		.button_container {
			padding: 30px;
		}
	}
</style>
